﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Web;
using HopNguyenCore.Extensions;

namespace HopNguyenCore.Core
{
    public class TypeProducts
    {
        readonly HopNguyenEntities _db = new HopNguyenEntities();
        public List<TypeProduct> GetAll()
        {
            try
            {
                return _db.TypeProducts.OrderByDescending(a => a.Sort).ThenByDescending(a => a.Id).ToList();
            }
            catch (Exception ex)
            {
                SystemLogs.WriteLog(ex);
                throw;
            }
        }

        public TypeProduct GetById(int id)
        {
            try
            {
                return _db.TypeProducts.SingleOrDefault(a => a.Id == id);
            }
            catch (Exception ex)
            {
                SystemLogs.WriteLog(ex);
                throw;
            }
        }
        public TypeProduct GetByAlias(string alias)
        {
            try
            {
                return _db.TypeProducts.FirstOrDefault(a => a.Alias == alias);
            }
            catch (Exception ex)
            {
                SystemLogs.WriteLog(ex);
                throw;
            }
        }

        public bool CheckName(string name, int id)
        {
            try
            {
                var list = _db.TypeProducts.Where(a => a.Id != id && a.Name.Equals(name)).ToList();
                if (list.Count > 0)
                {
                    return false;
                }
                return true;
            }
            catch (Exception ex)
            {
                SystemLogs.WriteLog(ex);
                throw;
            }
        }

        public string CheckAlias(string alias)
        {
            try
            {
                var list = _db.TypeProducts.Where(a => a.Alias.Equals(alias)).ToList();
                int i = 0;
                while (list.Any())
                {
                    i++;
                    alias = alias + "-" + i;
                }
                return alias;
            }
            catch (Exception ex)
            {
                SystemLogs.WriteLog(ex);
                throw;
            }
        }


        #region load multi level combobox
        public List<TypeProduct> GetMultiLevel()
        {
            try
            {
                var list = new List<TypeProduct>();
                foreach (var item in GetParentNull())
                {
                    list.AddRange(GetAllTypeByParent(item));
                }
                foreach (var item in list)
                {
                    var list3 = GetAllParentOfTypeProduct(item.Id);
                    if (list3 != null && list3.Count > 1)
                    {
                        for (int j = 0; j < (list3.Count - 1); j++)
                        {
                            item.Name = " - - - " + item.Name;
                        }
                    }
                }
                return list;
            }
            catch (Exception ex)
            {
                SystemLogs.WriteLog(ex);
                throw;
            }
        }

        public List<TypeProduct> GetParentNull()
        {
            try
            {
                return _db.TypeProducts.Where(a => a.Parent == null).ToList();
            }
            catch (Exception ex)
            {
                    SystemLogs.WriteLog(ex);
                throw;
            }
        }

        public List<TypeProduct> GetAllTypeByParent(TypeProduct parent)
        {
            try
            {
                var list = new List<TypeProduct>();
                var source = _db.TypeProducts.Where(a => a.Parent == parent.Id); 
                list.Add(parent);
                if (source.Any())
                {
                    foreach (var item in source)
                    {
                        list.AddRange(GetAllTypeByParent(item));
                    }
                }
                return list;
            }
            catch (Exception ex)
            {
                SystemLogs.WriteLog(ex);
                throw;
            }
            
        }
        public List<TypeProduct> GetAllParentOfTypeProduct(int typeproductid)
        {
            try
            {
                var list = new List<TypeProduct>();
                var item = _db.TypeProducts.Where(a => a.Id.Equals(typeproductid)).Take(1).SingleOrDefault();
                if (item != null && item.Parent.HasValue)
                {
                    list = GetAllParentOfTypeProduct(item.Parent.Value);
                    list.Add(item);
                }
                else
                {
                    list.Add(item);
                }
                return list;
            }
            catch (Exception ex)
            {
                SystemLogs.WriteLog(ex);
                throw;
            }
        }
        #endregion

        public bool Update(TypeProduct tp, HttpPostedFileBase file)
        {
            try
            {
                var obj = _db.TypeProducts.Find(tp.Id);
                if (obj != null)
                {
                    if (CheckName(tp.Name, tp.Id))
                    {
                        obj.Name = tp.Name;
                        obj.Parent = tp.Parent;
                        if (file != null)
                        {
                            obj.Image = Tool.UploadImage(file);
                        }
                        obj.Status = tp.Status;
                        obj.Sort = tp.Sort;
                        obj.Summary = tp.Summary;
                        obj.MetaTitle = tp.MetaTitle;
                        obj.MetaDescription = tp.MetaDescription;
                        _db.SaveChanges();
                    }
                    else
                    {
                        return false;
                    }
                }
                return true;
            }
            catch (Exception ex)
            {
                SystemLogs.WriteLog(ex);
                throw;
            }
        }
    }
}
